package com.mx.project.interceptor;

import com.auth0.jwt.exceptions.AlgorithmMismatchException;
import com.auth0.jwt.exceptions.SignatureVerificationException;
import com.auth0.jwt.exceptions.TokenExpiredException;
import com.mx.project.common.exception.CodeEnum;
import com.mx.project.common.exception.JKException;
import com.mx.project.common.util.JWTService;
import lombok.extern.log4j.Log4j2;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Log4j2
public class LoginInterceptor implements HandlerInterceptor{
	
	private JWTService jwtService;
	
	public LoginInterceptor(JWTService jwtService) {
		this.jwtService = jwtService;
	}
	
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object o) throws Exception {
		log.info("Token Checkout processing");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Content-Type", "text/html;charset=UTF-8");
		response.setHeader("Access-Control-Allow-Credentials", "true");
		response.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Requested-With, token");
		response.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT, DELETE");
		response.setHeader("Access-Control-Max-Age", "3600");
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
		String token = request.getHeader("token");
		System.out.println(request.getRequestURL());
		if (StringUtils.isEmpty(token)) {
			throw new JKException(CodeEnum.TOKEN_IS_EMPTY);
		}

		String  tokenInServletContext = (String)request.getServletContext().getAttribute(token);
		if(StringUtils.isEmpty(tokenInServletContext)) {
			throw new JKException(CodeEnum.ILLEGAL_TOKEN);
		}
		try {
			 jwtService.verifyToken(token);
		} catch (AlgorithmMismatchException  e) {
			log.error("Token Checkout processing AlgorithmMismatchException 异常！"+e.getLocalizedMessage());
			throw new JKException(CodeEnum.ILLEGAL_TOKEN);
		}catch (TokenExpiredException  e) {
			log.info("token已经过期");
			throw new JKException(CodeEnum.EXPIRE_TOKEN);
		}catch (SignatureVerificationException  e) {
			log.error("Token Checkout processing SignatureVerificationException 异常！"+e.getLocalizedMessage());
			throw new JKException(CodeEnum.ILLEGAL_TOKEN);
		 }catch (Exception e) {
			log.error("Token Checkout processing 未知异常！"+e.getLocalizedMessage());
			throw new JKException(CodeEnum.ILLEGAL_TOKEN);
		}
		return true;
	}

	@Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
		httpServletResponse.setHeader("Access-Control-Allow-Credentials", "true");
		httpServletResponse.setHeader("Access-Control-Allow-Headers", "Authorization, Content-Type, X-Requested-With, token");
		httpServletResponse.setHeader("Access-Control-Allow-Methods", "GET, HEAD, OPTIONS, POST, PUT, DELETE");
		httpServletResponse.setHeader("Access-Control-Max-Age", "3600");
    }
}
